package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.base.bean.TaskWorkPlan;
import com.jy.adscm.operation.base.bean.TaskWorkPlanDetail;
import com.jy.adscm.operation.base.mapper.mysql.TaskWorkPlanDetailMapper;
import com.jy.adscm.operation.base.mapper.mysql.TaskWorkPlanMapper;
import com.jy.adscm.operation.base.service.TaskWorkPlanDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 个人工作计划详情 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2020-05-26
 */
@Service
public class TaskWorkPlanDetailServiceImpl extends ServiceImpl<TaskWorkPlanDetailMapper, TaskWorkPlanDetail> implements TaskWorkPlanDetailService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    TaskWorkPlanMapper taskWorkPlanMapper;
    @Override
    public boolean insertTaskWorkPlanDetail(TaskWorkPlanDetail taskWorkPlanDetail, Token token) {
        taskWorkPlanDetail.setCreator(token.getUser().getId().toString());
        taskWorkPlanDetail.setCreateTime(new Date());
        //保存多选
        List<Integer> resultTypes = taskWorkPlanDetail.getResultTypes();
        if (resultTypes!=null && resultTypes.size()>0){
            StringBuffer sb=new StringBuffer();

            for (int i = 0; i < resultTypes.size(); i++) {
                if (i==0){
                    sb.append(resultTypes.get(i).toString());
                }else {
                    sb.append("-"+resultTypes.get(i).toString());
                }

            }
            taskWorkPlanDetail.setResultType(sb.toString());
        }



        boolean insert = super.insert(taskWorkPlanDetail);
        return insert;
    }

    @Override
    public boolean updateTaskWorkPlanDetail(TaskWorkPlanDetail taskWorkPlanDetail, Token token) {
         //如果有一条不审核不通过修改主页上的审核不通过
//        if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlanDetail.getState(),null)){
//            if (taskWorkPlanDetail.getState()==3){
//                TaskWorkPlan taskWorkPlan = taskWorkPlanMapper.selectById(taskWorkPlanDetail.getPid());
//                taskWorkPlan.setState(3);
//                taskWorkPlanMapper.updateById(taskWorkPlan);
//            }
//        }
        List<Integer> resultTypes = taskWorkPlanDetail.getResultTypes();
        if (resultTypes!=null && resultTypes.size()>0){
            StringBuffer sb=new StringBuffer();

            for (int i = 0; i < resultTypes.size(); i++) {
                if (i==0){
                    sb.append(resultTypes.get(i).toString());
                }else {
                    sb.append("-"+resultTypes.get(i).toString());
                }
            }
            taskWorkPlanDetail.setResultType(sb.toString());
        }
        boolean b = super.updateById(taskWorkPlanDetail);
        return b;
    }

    @Override
    public boolean deleteTaskWorkPlanDetail(Long id, Token token) {
        Boolean result=false;
        TaskWorkPlanDetail taskWorkPlanDetail = super.selectById(id);
        if (taskWorkPlanDetail.getCreator().equals(token.getUser().getId().toString())){
            result = super.deleteById(id);
        }
        return result;
    }

    @Override
    public List<TaskWorkPlanDetail> selectByPid(Long id) {
        Wrapper<TaskWorkPlanDetail> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",id);
        List<TaskWorkPlanDetail> taskWorkPlanDetails = super.selectList(wrapper);
        for (TaskWorkPlanDetail taskWorkPlanDetail : taskWorkPlanDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlanDetail.getApprove(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskWorkPlanDetail.getApprove());
                taskWorkPlanDetail.setApproveName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlanDetail.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(taskWorkPlanDetail.getCreator());
                taskWorkPlanDetail.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkPlanDetail.getResultType(),null)){
                String[] split = taskWorkPlanDetail.getResultType().split("-");
                List<Integer> resultTypes =new ArrayList<>();
                for (String s : split) {
                    resultTypes.add(Integer.valueOf(s));
                }
                taskWorkPlanDetail.setResultTypes(resultTypes);
            }
        }
        return taskWorkPlanDetails;
    }
}
